package com.myblog.demo.controller;

import com.myblog.demo.common.AjaxResult;
import com.myblog.demo.common.UserSessionTools;
import com.myblog.demo.entity.UserInfo;
import com.myblog.demo.entity.vo.Following;
import com.myblog.demo.entity.vo.Follows;
import com.myblog.demo.service.FollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/follow")
public class FollowController {
    @Autowired
    private FollowService followService;

    /**
     * 当前登录用户新增关注 followingId 用户
     *
     * @param followingId 要关注的用户id
     * @param request     HttpServletRequest
     * @return AjaxResult
     */
    @RequestMapping("/add_follow_relationship")
    public AjaxResult addFollowRelationship(Integer followingId, HttpServletRequest request) {
        // 参数校验
        if (followingId == null || followingId < 0) {
            return AjaxResult.fail(-1, "参数错误！");
        }

        // 获取当前登录用户的id
        UserInfo loginUser = UserSessionTools.getLoginUser(request);
        if (loginUser == null) {
            return AjaxResult.fail(-2, "当前用户未登录");
        }

        int userId = loginUser.getId();

        // 数据库持久化，服务层判断当前登录用户是否已经关注目标用户
        int result = followService.addFollowRelationship(userId, followingId);

        return AjaxResult.success(result);
    }

    /**
     * 当前登录用户要取消关注 followingId 用户
     *
     * @param followingId 要取消关注的用户
     * @param request     HttpServletRequest
     * @return AjaxResult
     */
    @RequestMapping("/del_follow_relationship")
    public AjaxResult delFollowRelationship(Integer followingId, HttpServletRequest request) {
        // 参数校验
        if (followingId == null || followingId < 0) {
            return AjaxResult.fail(-1, "参数错误！");
        }

        // 获取当前登录用户的id
        UserInfo loginUser = UserSessionTools.getLoginUser(request);
        if (loginUser == null) {
            return AjaxResult.fail(-2, "当前用户未登录");
        }

        int userId = loginUser.getId();

        // 从数据库中删除
        int result = followService.delFollowRelationship(userId, followingId);

        return AjaxResult.success(result);
    }

    /**
     * 获取 userId 用户的关注者列表
     *
     * @param userId userId
     * @return AjaxResult
     */
    @RequestMapping("/get_following_list")
    public AjaxResult getFollowingList(Integer userId) {
        // 参数校验
        if (userId == null || userId <= 0) {
            return AjaxResult.fail(-1, "参数错误！");
        }

        // 查询数据库
        List<Following> list = followService.getFollowingList(userId);

        return AjaxResult.success(list);
    }

    /**
     * 获取 userId 用户的粉丝列表
     *
     * @param userId userId
     * @return AjaxResult
     */
    @RequestMapping("/get_follows_list")
    public AjaxResult getFollowsList(Integer userId) {
        // 参数校验
        if (userId == null || userId <= 0) {
            return AjaxResult.fail(-1, "参数错误！");
        }

        // 查询数据库
        List<Follows> list = followService.getFollowsList(userId);
        return AjaxResult.success(list);
    }

    // 判断当前用户是否关注 followingId 用户
    @RequestMapping("/is_following")
    public  AjaxResult isFollowing(Integer followingId, HttpServletRequest request){
        // 参数校验
        if (followingId == null || followingId < 0) {
            return AjaxResult.fail(-1, "参数错误！");
        }

        // 获取当前登录用户的id
        UserInfo loginUser = UserSessionTools.getLoginUser(request);
        if (loginUser == null) {
            return AjaxResult.fail(-2, "当前用户未登录");
        }

        int userId = loginUser.getId();

        // 查询数据库
        Boolean result = followService.isFollowing(userId, followingId);

        return AjaxResult.success(result);
    }
}
